<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="${comment}" prop="fid">
        <el-input
          v-model="queryParams.fid"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="topicId">
        <el-input
          v-model="queryParams.topicId"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="authorid">
        <el-input
          v-model="queryParams.authorid"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="dateline">
        <el-date-picker clearable
          v-model="queryParams.dateline"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="${comment}" prop="views">
        <el-input
          v-model="queryParams.views"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="postCount">
        <el-input
          v-model="queryParams.postCount"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="replyCount">
        <el-input
          v-model="queryParams.replyCount"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isSticky">
        <el-input
          v-model="queryParams.isSticky"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isDigest">
        <el-input
          v-model="queryParams.isDigest"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isRecommended">
        <el-input
          v-model="queryParams.isRecommended"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isHot">
        <el-input
          v-model="queryParams.isHot"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="isEnable">
        <el-input
          v-model="queryParams.isEnable"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="tags">
        <el-input
          v-model="queryParams.tags"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="lastpost">
        <el-date-picker clearable
          v-model="queryParams.lastpost"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bbs:thread:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bbs:thread:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bbs:thread:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bbs:thread:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="threadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="tid" />
      <el-table-column label="${comment}" align="center" prop="fid" />
      <el-table-column label="${comment}" align="center" prop="topicId" />
      <el-table-column label="${comment}" align="center" prop="title" />
      <el-table-column label="${comment}" align="center" prop="authorid" />
      <el-table-column label="${comment}" align="center" prop="dateline" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="views" />
      <el-table-column label="${comment}" align="center" prop="postCount" />
      <el-table-column label="${comment}" align="center" prop="replyCount" />
      <el-table-column label="${comment}" align="center" prop="isSticky" />
      <el-table-column label="${comment}" align="center" prop="isDigest" />
      <el-table-column label="${comment}" align="center" prop="isRecommended" />
      <el-table-column label="${comment}" align="center" prop="isHot" />
      <el-table-column label="${comment}" align="center" prop="url" />
      <el-table-column label="${comment}" align="center" prop="type" />
      <el-table-column label="${comment}" align="center" prop="isEnable" />
      <el-table-column label="${comment}" align="center" prop="tags" />
      <el-table-column label="${comment}" align="center" prop="lastpost" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastpost, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bbs:thread:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bbs:thread:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="fid">
          <el-input v-model="form.fid" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="topicId">
          <el-input v-model="form.topicId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="title">
          <el-input v-model="form.title" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="authorid">
          <el-input v-model="form.authorid" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="dateline">
          <el-date-picker clearable
            v-model="form.dateline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="views">
          <el-input v-model="form.views" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="postCount">
          <el-input v-model="form.postCount" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="replyCount">
          <el-input v-model="form.replyCount" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isSticky">
          <el-input v-model="form.isSticky" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isDigest">
          <el-input v-model="form.isDigest" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isRecommended">
          <el-input v-model="form.isRecommended" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isHot">
          <el-input v-model="form.isHot" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="url">
          <el-input v-model="form.url" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="isEnable">
          <el-input v-model="form.isEnable" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="tags">
          <el-input v-model="form.tags" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="lastpost">
          <el-date-picker clearable
            v-model="form.lastpost"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">主题内容信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBbsThreadContent">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBbsThreadContent">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="bbsThreadContentList" :row-class-name="rowBbsThreadContentIndex" @selection-change="handleBbsThreadContentSelectionChange" ref="bbsThreadContent">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="$comment" prop="dateline" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.dateline" type="date" value-format="yyyy-MM-dd" placeholder="请选择$comment" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listThread, getThread, delThread, addThread, updateThread } from "@/api/bbs/thread";

export default {
  name: "Thread",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBbsThreadContent: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主题表格数据
      threadList: [],
      // 主题内容表格数据
      bbsThreadContentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fid: null,
        topicId: null,
        title: null,
        authorid: null,
        dateline: null,
        views: null,
        postCount: null,
        replyCount: null,
        isSticky: null,
        isDigest: null,
        isRecommended: null,
        isHot: null,
        url: null,
        type: null,
        isEnable: null,
        tags: null,
        lastpost: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fid: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        topicId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        authorid: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询主题列表 */
    getList() {
      this.loading = true;
      listThread(this.queryParams).then(response => {
        this.threadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tid: null,
        fid: null,
        topicId: null,
        title: null,
        authorid: null,
        dateline: null,
        views: null,
        postCount: null,
        replyCount: null,
        isSticky: null,
        isDigest: null,
        isRecommended: null,
        isHot: null,
        url: null,
        type: null,
        isEnable: null,
        tags: null,
        lastpost: null
      };
      this.bbsThreadContentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加主题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tid = row.tid || this.ids
      getThread(tid).then(response => {
        this.form = response.data;
        this.bbsThreadContentList = response.data.bbsThreadContentList;
        this.open = true;
        this.title = "修改主题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.bbsThreadContentList = this.bbsThreadContentList;
          if (this.form.tid != null) {
            updateThread(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addThread(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tids = row.tid || this.ids;
      this.$modal.confirm('是否确认删除主题编号为"' + tids + '"的数据项？').then(function() {
        return delThread(tids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 主题内容序号 */
    rowBbsThreadContentIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 主题内容添加按钮操作 */
    handleAddBbsThreadContent() {
      let obj = {};
      obj.content = "";
      obj.dateline = "";
      this.bbsThreadContentList.push(obj);
    },
    /** 主题内容删除按钮操作 */
    handleDeleteBbsThreadContent() {
      if (this.checkedBbsThreadContent.length == 0) {
        this.$modal.msgError("请先选择要删除的主题内容数据");
      } else {
        const bbsThreadContentList = this.bbsThreadContentList;
        const checkedBbsThreadContent = this.checkedBbsThreadContent;
        this.bbsThreadContentList = bbsThreadContentList.filter(function(item) {
          return checkedBbsThreadContent.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBbsThreadContentSelectionChange(selection) {
      this.checkedBbsThreadContent = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bbs/thread/export', {
        ...this.queryParams
      }, `thread_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
